package com.sesame.user.service;

import com.sesame.user.bean.User;
import com.sesame.user.dao.UserDao;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * UserService
 * @author admin
 * @date 2019-01-30 13:03:18
 * @Description: 
 */
@CommonsLog
@Service
public class UserService {

    @SuppressWarnings("all")
	@Resource
	private UserDao userDao;

	/**
	 * 查询list
	 * @author admin
     * @date 2019-01-30 13:03:18
     * @Description: 分页时要注意
	 */
	public List<User> searchList(User bean) {
		
		List<User> list = userDao.searchList(bean);

		return list;
	}
	
	/**
	 * 新增
	 * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:	
	 */
	@Transactional(rollbackFor = Exception.class)
	public int add(User bean)  {

		int res = userDao.insert(bean);
		
		return res;
	}
	
	/**
	 * 修改
	 * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:	
	 */
	@Transactional(rollbackFor = Exception.class)
	public int update(User bean)  {

		int res = userDao.update(bean);
		
		return res;
	}
	
	/**
	 * 删除
	 * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:	
	 */
	@Transactional(rollbackFor = Exception.class)
    public int delete(String ids) {

        List<String> list_id = Stream.of(ids.split(",")).map(String::trim).distinct().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        list_id.stream().forEach(this::deleteById);

        return list_id.size();
    }
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
         //物理删除
        userDao.delete(id);

        //逻辑删除
//    User bean = new User();
//    	bean.setId(id);
//    	bean.setActive(GData.BOOLEAN.NO);
//    	bean.setUpdateTime(new Date());
//    	update(bean);
    }
	
	/**
	 * 查询--返回bean
	 * @author admin
     * @date 2019-01-30 13:03:18
     * @Description:	
	 */
	public User search(User bean)  {

		return userDao.search(bean);
	}

	public Map searchMap(User bean){
		return userDao.searchMap(bean);
	}
	public Map searchGMap(User bean){
		return userDao.searchGMap(bean);
	}
}
